package com.timanetworks.audi.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;

public class HadoopFileService {

    private static Configuration conf = null;

    public String defaultAddress = "webhdfs://10.224.32.20:50070/";//设置hdfs的连接方式为webhdfs，通过HTTP访问hdfs


    private HadoopFileService() {
        init();
    }

    public void init(){
        conf = new Configuration();
        System.setProperty("java.security.krb5.conf","D:\\conf\\tima\\national\\krb5.conf");//设置kerberos配置信息
        conf.set("fs.defaultFS", defaultAddress);//namenode地址
        conf.set("fs.hdfs.impl",org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
        conf.set("fs.file.impl",org.apache.hadoop.fs.LocalFileSystem.class.getName());
        conf.set("fs.webhdfs.impl",org.apache.hadoop.hdfs.web.WebHdfsFileSystem.class.getName());
        conf.setBoolean("hadoop.security.authentication",false);
        conf.set("hadoop.security.authentication","kerberos");
        conf.set("dfs.namenode.kerberos.principal","hdfs/_HOST@UAT.COM");//hdfs-site.xml中配置信息
        conf.set("dfs.datanode.kerberos.principal","hdfs/_HOST@UAT.COM");//hdfs-site.xml中配置信息
        UserGroupInformation.setConfiguration(conf);
        try {
            UserGroupInformation.loginUserFromKeytab("audi","D:\\conf\\tima\\national\\audi.keytab");//kerberos 认证
            UserGroupInformation.getLoginUser();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public boolean exits(String path) throws Exception {

        FileSystem fs = FileSystem.get(conf);
        return fs.exists(new Path(path));
    }



    public static void main(String[] args){
        HadoopFileService hd = new HadoopFileService();
        try {
            System.out.println(hd.exits("/"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
